﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InterfaceFiles;

namespace SimpleInterfaceConverter
{
  public partial class Main : Form
  {
    public Main()
    {
      InitializeComponent();
    }

    private void txtInputFile_EditorButtonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
    {
      if (dlgOpen.ShowDialog() == DialogResult.OK)
      {
        txtInputFile.Text = dlgOpen.FileName;
      }
    }

    private void txtOutputFile_EditorButtonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
    {
      if (dlgSave.ShowDialog() == DialogResult.OK)
      {
        txtOutputFile.Text = dlgSave.FileName;
      }
    }

    private void btnClose_Click(object sender, EventArgs e)
    {
      Close();
    }

    private void getInputInterfaceFile(ref IInterfaceFile inputFile, string inputFileName)
    {
      switch (cmbInputFormat.Text)
      {
        case "PDX SWMM 4":
          inputFile = new PDXSWMM4InterfaceFile(inputFileName);
          break;
        case "XP SWMM 10":
          inputFile = new XPSWMM10InterfaceFile(inputFileName);
          break;
      }
    }
    private void getOutputInterfaceFile(ref IInterfaceFile outputFile, string outputFileName)
    {
      switch (cmbOutputFormat.Text)
      {
        case "EPA SWMM 5 Text":
          outputFile = new EPASWMM5InterfaceFile(outputFileName);
          break;
        case "Text CSV":
          outputFile = new TextInterfaceFile(outputFileName);
          break;
      }
    }
    private void btnGo_Click(object sender, EventArgs e)
    {
      IInterfaceFile inputFile = null;
      string inputFileName = txtInputFile.Text;
      getInputInterfaceFile(ref inputFile, inputFileName);

      IInterfaceFile outputFile = null;
      string outputFileName = txtOutputFile.Text;
      getOutputInterfaceFile(ref outputFile, outputFileName);

      if (outputFile != null && inputFile != null)
      {
        this.Cursor = Cursors.WaitCursor;
        try
        {
          outputFile.WriteInterface(inputFile);
        }
        finally
        {
          this.Cursor = Cursors.Default;
        }
      }
    }

    private void CheckUIEnabled()
    {
      btnGo.Enabled =
        (txtInputFile.Text != string.Empty &&
        txtOutputFile.Text != string.Empty &&
        cmbInputFormat.Text != string.Empty &&
        cmbOutputFormat.Text != string.Empty);
    }

    private void txtInputFile_ValueChanged(object sender, EventArgs e)
    {
      CheckUIEnabled();
    }

    private void Main_Load(object sender, EventArgs e)
    {
      CheckUIEnabled();
      cmbInputFormat.Text = "PDX SWMM 4";
      cmbOutputFormat.Text = "EPA SWMM 5 Text";
    }
  }
}
